package com.authen.facecompare.activity;

import android.app.Activity;
import android.content.Context;


import com.example.rizhaoscienceandtechnologymuseum.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class ModelInit {
	public static Map<String,byte[]> lstFeature;
	public static Map<Integer, String> modelDirMap;

	// 初始化模型数据
	public static Map<Integer, String> getModelDirMap(Activity cx) {
		Map<Integer, String> modelMap = new HashMap<Integer, String>();
		modelMap.put(R.raw.detect, "Detect.pkg");
		modelMap.put(R.raw.lm, "Landmark.pkg");
		modelMap.put(R.raw.extract, "Extract.pkg");
		modelMap.put(R.raw.anti, "Anti.pkg");
//		modelMap.put(R.raw.anticolor, "ColorFaceNCNN.pkg");
//		modelMap.put(R.raw.prop, "Prop.pkg");

		if(lstFeature == null)
		    lstFeature = new HashMap<>();

		modelDirMap = new HashMap<Integer, String>();

		for (Integer key : modelMap.keySet()) {
			String outputFileName = modelMap.get(key);
			try {

				InputStream inputStream = cx.getResources().openRawResource(key);
				File outputDir = cx.getDir("model", Context.MODE_PRIVATE);
				System.out.println("filePath:" + outputDir.getAbsolutePath());
				File outputFile = new File(outputDir, outputFileName);

				FileOutputStream outputStream = new FileOutputStream(outputFile);
				byte[] buffer = new byte[inputStream.available()];
				int bytesRead;
				while ((bytesRead = inputStream.read(buffer)) != -1) {
					outputStream.write(buffer, 0, bytesRead);
				}
				inputStream.close();
				outputStream.close();
				modelDirMap.put(key, outputDir.getAbsolutePath());

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return modelDirMap;
	}

	public static void initFras() {
		try {
			System.loadLibrary("fras-jni");
			// System.loadlibar
		} catch (UnsatisfiedLinkError e) {
			System.out.println("face recognition library not found!");
		}
	}

	public static void initAnti() {
		try {
			System.loadLibrary("antispoof-jni");
			// System.loadlibar
		} catch (UnsatisfiedLinkError e) {
			System.out.println("face recognition library not found!");
		}
	}

	public static Map<String, byte[]> getLstFeature() {
		if(lstFeature == null)
			lstFeature = new HashMap<>();
		return lstFeature;
	}
}
